<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Area;
use yii\db\Query;



class PositionLogic
{
    public function getCityList()
    {
        return Area::find()
            ->select('region_code,region_name,firstchar')
            ->from('{{%area}} a')
            ->rightJoin('{{%shop}} s', 's.city_id=a.region_code AND s.status=1')
            ->where('a.region_level=2')
            ->asArray()
            ->all();
    }

    public function getDistrictList($region_code)
    {
        return Area::find()
            ->select('region_code,region_name')
            ->from('{{%area}} a')
            ->rightJoin('{{%shop}} s', 's.district_id=a.region_code AND s.status=1')
            ->where("a.region_level=3 AND a.parent_code=$region_code")
            ->asArray()
            ->all();
    }

    public function getStreetList($region_code)
    {
        return Area::find()
            ->select('region_code,region_name,remark,count(s.shop_id) as shop_count')
            ->from('{{%area}} a')
            ->rightJoin('{{%shop}} s', 's.street_id=a.region_code AND s.status=1')
            ->where("a.region_level=4 AND a.parent_code=$region_code")
            ->groupBy('s.street_id')
            ->asArray()
            ->all();
    }

    public function getRegionCode($city,$district,$street){
        $cityresult = Area::find()
            ->where("`region_name` LIKE '%$city%' AND region_level=2")
            ->asArray()
            ->one();
        $districtresult = Area::find()
            ->where("`region_name` LIKE '%$district%' AND parent_code=".$cityresult['region_code'])
            ->asArray()
            ->one();
        /*$streetresult = Area::find()
            ->where("`region_name` LIKE '%$street%' AND parent_code=".$districtresult['region_code'])
            ->asArray()
            ->one();*/
        return $districtresult['region_code'];
    }






}